﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Exceptions;
using Serilog.Sinks.Email;
using Sejil;
namespace LogDemo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddLogging(loggerBuilder =>
            {
                EmailConnectionInfo info = new EmailConnectionInfo()
                {
                    EmailSubject = "Email subject",
                    FromEmail = "757365800@qq.com",
                    MailServer = "smtp.qq.com",
                    NetworkCredentials = new NetworkCredential("757365800@qq.com", "aiwang921002"),
                    Port = 587,
                    ToEmail = "lyl1002@outlook.com"
                };
                loggerBuilder.AddSerilog(
                    new LoggerConfiguration()
                    .WriteTo.Email(info)
                    //.WriteTo
                    // .Email(
                    // networkCredential: new NetworkCredential("757365800@qq.com", "aiwang921002"),
                    // fromEmail: "757365800@qq.com",
                    // toEmail: "lyl1002@outlook.com",
                    // mailServer: "smtp.qq.com"
                    // )
                     .Enrich.WithExceptionDetails()
                     .ReadFrom.Configuration(Configuration).CreateLogger());
            });

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSejil();
            app.UseMvc();
        }
    }
}
